//index.js
//获取应用实例
var app = getApp()
Page({
  data: {
    page:0,
    showLoading: true,
    noMoretip: false,
    activityStatus:'2'
  },
  onLoad: function (e) {
    var that =this;
    if (e && e.enterfrom) {
      var enterfrom = e.enterfrom == 'share' ? true : false;
      that.setData({
        enterfrom: enterfrom
      })
    }
  },
  onShow: function () {
    var that = this;
    if (!app.globalData.plumSession) {
      app.wechatSq(that);
    }else{
      that.requestIndex();
      that.requestBargainlist();
      app.requestAd(that);
    }
  },
  requestIndex: function () {
    var that = this;
    wx.request({
      url: app.globalData.requestUrl,
      data: {
        map: 'applet_bargain_index'
      },
      success: function (res) {
        console.log(res.data);
        if (res.data.ec == 200) {
          if (res.data.data.template.title) {
            wx.setNavigationBarTitle({
              title: res.data.data.template.title
            });
          }
          var categoryList = app.splitArrData(res.data.data.shortcut, 10);
          that.setData({
            indexInfo: res.data.data,
            category: res.data.data.shortcut,
            categoryList: categoryList,
            slideImgUrls: res.data.data.slide
          })
          that.getPostTabtop();
        } else {
        }
      },
      complete: function () {
        wx.stopPullDownRefresh();
      }
    });
  },
  openLink: function (e) {
    var that = this;
    var link = e.currentTarget.dataset.link;
    // var type = e.currentTarget.dataset.type;;
    // console.log();
    // if (type != 106) {
    //   wx.navigateTo({
    //     url: link
    //   });
    // }
    if (link) {
      wx.navigateTo({
        url: link
      });
    }
  },
  requestBargainlist: function () {
    var that = this;
    var page = that.data.page;
    var data = {
      map: 'applet_bargain_activity_list',
      page: page,
      timeStatus: that.data.activityStatus
    }
    console.log(data);
    //发起请求，获取列表列表
    wx.showLoading({
      title: '加载中',
    })
    wx.request({
      url: app.globalData.requestUrl,
      data: data,
      success: function (res) {
        console.log(res.data.data);
        if (res.data.ec == 200) {
          var allArr = [];
          var initArr = that.data.bargainGoodslist;
          var curArr = res.data.data;
          var lastPageLength = curArr.length;
          if (page > 0) {
            allArr = initArr.concat(curArr);
          } else {
            allArr = res.data.data;
          }
          that.setData({
            bargainGoodslist: allArr
          })
          if (lastPageLength < 10) {
            that.setData({
              noMoretip: true,
              showLoading: false
            });
          }
        } else {
          console.log(res.data)
          if (page <= 0) {
            that.setData({
              bargainGoodslist: [],
              showLoading: false
            })
          } else {
            that.setData({
              noMoretip: true,
              showLoading: false
            });
          }
        }
      },
      complete: function () {
        wx.hideLoading();
        wx.stopPullDownRefresh();
      }
    });
  },
  onPullDownRefresh: function () {
    this.setData({
      page: 0,
      noMoretip: false,
      showLoading: true
    });
    this.onShow();
    this.onLoad();
    console.log("下拉刷新");
  },
  onReachBottom: function () {
    var that = this;
    console.log("到达页面底部")
    var isMore = that.data.noMoretip;
    var page = that.data.page;
    page++;
    that.setData({
      page: page
    });
    if (isMore) {
      console.log("已完成或正在加载");
    } else {
      that.requestBargainlist();
    }
  },
  tobargainGoodDetail:function(e){//砍价商品详情
    var that = this;
    var id = e.currentTarget.dataset.id;
    wx.navigateTo({
      url: '/subpages/bargainGoodDetail/bargainGoodDetail?id='+id
    })
  },
  tomyParticipate:function(){
    wx.navigateTo({
      url: '/subpages/myParticipate/myParticipate',
    })
  },
  tomyOrder: function () {
    wx.navigateTo({
      url: '/subpages/bargainOrder/bargainOrder',
    })
  },
  toCuslink: function (e) {
    var link = e.currentTarget.dataset.link;
    console.log(link);
    wx.navigateTo({
      url: link
    })
  },
  toggleStatus:function(e){
    var that = this;
    var status = e.currentTarget.dataset.status;
    that.setData({
      page: 0,
      noMoretip: false,
      showLoading: true,
      activityStatus: status,
      bargainGoodslist:null
    })
    that.requestBargainlist();
  },
  getPostTabtop: function () {
    var that = this;
    console.log("获取固定tab top---");
    wx.createSelectorQuery().select('#topdistance').boundingClientRect(function (rect) {
      that.setData({
        posttabTop: rect.height
      })
      console.log(that.data.posttabTop);
    }).exec()
  },
  onPageScroll: function (e) {
    var that = this;
    var scrollTop = e.scrollTop;
    var posttabTop = that.data.posttabTop;
    if (scrollTop > 480 && that.data.showBacktop!=1){
      that.setData({ showBacktop: 1 })
    } else if (scrollTop <= 480 && that.data.showBacktop != 0){
      that.setData({ showBacktop: 0 })
    }
    if (scrollTop >= posttabTop && that.data.isFixed != 1) {
      that.setData({ isFixed: 1 })
    } else if (scrollTop < posttabTop && that.data.isFixed != 0) {
      that.setData({ isFixed: 0 })
    }
  },
  onShareAppMessage: function () {
    var title = this.data.title;
    var shareInfo = app.globalData.shareInfo;
    title = shareInfo.shareTitle ? shareInfo.shareTitle : title;
    var cover = shareInfo.shareCover ? shareInfo.shareCover : '';
    app.getPoint(that);
    return {
      title: title,
      imageUrl: cover,
      path: '/subpages/bargainIndexPage/bargainIndexPage?enterfrom=share'
    }
  },
})